package com.zxw.config;

import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.Retryer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.retry.backoff.ExponentialBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Component;

/**
 * @author zxw
 * @date 2022-01-31 11:19
 */
@Component
public class FeignConfig {

    @Bean
    public Retryer retryer() {
        return new Retryer.Default(1, 1, 3);
    }

    @Bean
    public RetryTemplate retryTemplate() {
        RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setRetryPolicy(new SimpleRetryPolicy(1));
        ExponentialBackOffPolicy backOffPolicy = new ExponentialBackOffPolicy();
        backOffPolicy.setInitialInterval(1000);
        backOffPolicy.setMultiplier(2);
        backOffPolicy.setMaxInterval(1000);
        retryTemplate.setBackOffPolicy(backOffPolicy);
        return retryTemplate;
    }

    @Bean
    public RequestInterceptor requestInterceptor() {
        return new DefineInterceptor();
    }

//    @Bean
//    public Client feignClient(BlockingLoadBalancerClient loadBalancerClient) {
//        return new FeignBlockingLoadBalancerClient(new Client.Default(null, null),
//                loadBalancerClient);
//    }

    @Bean
    public Request.Options options() {
        return new Request.Options(5000, 5000);
    }
}

@Slf4j
class DefineInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        log.info("打印拦截日志");
    }
}
